package domain;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import dao.UserBuyDao;
import model.UserBuy;

/**
 * 
 * @author Administrator
 *需求：双色球匹配问题
 *分析：双色球一共7个球，前六个为红球，最后一个为蓝球，优先级篮球最高。和中奖号码匹配时，首先匹配个数，个数相同的时候，比较最后一个蓝色球。
 *
 */
public class domain {
	public static void main(String[] args){
		UserBuyDao userBuyDao=new UserBuyDao();		
/*		//产生100个 userBuy
		int programId;
		System.out.println("请输入这是第几期双色球？");
		Scanner sc=new Scanner(System.in);
		programId=sc.nextInt();
		System.out.println("这是第"+programId+"期双色球");*/
		
/*		//购买双色球
		for(int i=0,count=0;i<100;i++){
			ArrayList<Integer> ball = generateRandomList();
			count++;
			UserBuy userBuy=new UserBuy();
			userBuy.setProgramId(programId);
			userBuy.setRed_1(ball.get(0));
			userBuy.setRed_2(ball.get(1));
			userBuy.setRed_3(ball.get(2));
			userBuy.setRed_4(ball.get(3));
			userBuy.setRed_5(ball.get(4));
			userBuy.setRed_6(ball.get(5));
			userBuy.setBlue(ball.get(6));
			System.out.println("您好，第"+count+"用户，您购买的双色球号码为："+ball);
			int flag=userBuyDao.addUserBuy(userBuy);
			if(flag==1){
				System.out.println("您购买成功！");
			}else{
				System.out.println("您购买失败！");
			}
			System.out.println("-----------------------");*/
			
            //最新一期中奖号码为：
			ArrayList<Integer> programNum=new ArrayList<Integer>(Arrays.asList(02,06,12,17,18,19,10));
			System.out.println("最新一期的中奖号码为：");
			System.out.println(programNum);
			
			//匹配个数
			ArrayList<UserBuy> listUserBuy=userBuyDao.findAllUserBuy();
			//System.out.println(listUserBuy);
			for(int i=0;i<listUserBuy.size();i++){
				UserBuy userBuy=new UserBuy();
				userBuy=listUserBuy.get(i);
				System.out.println("第"+userBuy.getUserId()+"用户，您好！");
				ArrayList<Integer> ballNum=new ArrayList<Integer>(Arrays.asList(
						userBuy.getRed_1(),
						userBuy.getRed_2(),
						userBuy.getRed_3(),
						userBuy.getRed_4(),
						userBuy.getRed_5(),
						userBuy.getRed_6(),
						userBuy.getBlue()));
				System.out.println("您购买的双色球为："+ballNum);
				Set<Integer> listSet = new HashSet<Integer>();
				listSet.addAll(ballNum);
				listSet.addAll(programNum);
				int marryNum = 14-listSet.size();
				System.out.println("您匹配到的中奖号码个数为："+marryNum);
				int marryBlue=(ballNum.get(6)==programNum.get(6))?1:0;
				if(marryBlue==1){
					System.out.println("您的蓝色球猜中了！");
				}else{
					System.out.println("很遗憾！您的蓝色球没有猜中！");	
				}
				int weight=marryNum*2+marryBlue;
				System.out.println("您的幸运指数是"+weight);
	
				int flag=userBuyDao.addMarry(userBuy.getUserId(), marryNum, marryBlue, weight);
				if(flag==1){
					System.out.println("祝君好运！");
				}else{
					System.out.println("再接再厉！");
				}
				System.out.println("---------------------");
			}
			
			//展示出最幸运的前10位用户
			listUserBuy.clear();
			listUserBuy=userBuyDao.findTopTen();
			for(int i=0;i<listUserBuy.size();i++){
				UserBuy userBuy=new UserBuy();
				userBuy=listUserBuy.get(i);
				System.out.println("第"+userBuy.getUserId()+"用户，您好！");
				System.out.println("恭喜您中得第"+userBuy.getProgramId()+"期双色球的第"+(i+1)+"名！");
				ArrayList<Integer> ballNum=new ArrayList<Integer>(Arrays.asList(
						userBuy.getRed_1(),
						userBuy.getRed_2(),
						userBuy.getRed_3(),
						userBuy.getRed_4(),
						userBuy.getRed_5(),
						userBuy.getRed_6(),
						userBuy.getBlue()));
				System.out.println("您购买的双色球为："+ballNum);
				System.out.println("您的幸运值为："+userBuy.getWeight()+"!");
				System.out.println("--------------------------------");
			}						
	}
		
		
	

	
	
	
	
	
	
	/**
	 * 冒泡排序
	 */

	public static void sort(ArrayList<Integer> list){
		int len=list.size();
		int temp=0;
		for(int i=0;i<len;i++){
			for (int j = i + 1; j < len; j++) {
                if (list.get(i) > list.get(j)) {
                    temp = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, temp);
                }
			}			
		}
	}
	
	  /**
     * 生成随机数
     * @param count 随机数个数
     * @return 随机数列表
     */
    public static ArrayList<Integer> generateRandomList() {
    	Random r=new Random();
		ArrayList<Integer> listRed = new ArrayList<Integer>();
		ArrayList<Integer> list = new ArrayList<Integer>(); 	
        int j;
        
		for (int i = 1; i <= 33; i++) {
			listRed.add(i);
		}
		
		for (int i = 1; i <= 6; i++) {
			// 在arrayList中产生一个指定下标
			int red = r.nextInt(listRed.size());
			//
			int a = listRed.get(red);
			list.add(a);
			listRed.remove(red);
			}
		
		Collections.sort(list);
		int blue = (r.nextInt(16) + 1);
		list.add(blue);		
		return list;
    }  
}

